﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Model;

namespace Web.Admin
{
    public partial class Tags_Modify : BaseCode.BaseAdmin
    {
        private DAL_Tags dalTags = new DAL_Tags();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int tid = int.Parse(Request.QueryString["id"].ToString());
                bind(tid);
                ViewState["tid"] = tid;
            }
        }

        public void bind(int tid)
        {
            DataTable tag = dalTags.GetTagByID(tid);
            this.txtName.Text = tag.Rows[0]["Name"].ToString();
            this.txtEname.Text = tag.Rows[0]["EName"].ToString();
            this.txtKeywords.Text = tag.Rows[0]["Keywords"].ToString();
            this.txtRemark.Text = tag.Rows[0]["Remark"].ToString();
            this.ddlType.SelectedValue = tag.Rows[0]["tagType"].ToString();
            this.rbtnNoUse.Checked = (int.Parse(tag.Rows[0]["State"].ToString()) == 0);
            this.rbtnUse.Checked = (int.Parse(tag.Rows[0]["State"].ToString()) == 1);
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string ename = this.txtEname.Text;
            string keyword = this.txtKeywords.Text;
            string remark = this.txtRemark.Text;
            string type = this.ddlType.SelectedValue;
            int state = (this.rbtnNoUse.Checked == true) ? 0 : 1;
            string tid = ViewState["tid"].ToString();

            Model_Tags tag = dalTags.GetTagById(int.Parse(tid));
            tag.name = name;
            tag.ename = ename;
            tag.keywords = keyword;
            tag.remark = remark;
            tag.tagtype = int.Parse(type);
            tag.state = state;
            tag.updatetime = DateTime.Now;
            if (dalTags.Update(tag) > 0)
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('修改成功！');</script>");
                Response.Redirect("Tags.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "提示", "<script>alert('修改失败！');</script>");
                return;
            }
            
        }
    }
}